
import 'package:connectivity_plus/connectivity_plus.dart';

class NetworkInfo {
  final Connectivity _connectivity = Connectivity();

  /// 检查是否有网络连接（WiFi/移动网络均可）
  Future<bool> isConnected() async {
    final result = await _connectivity.checkConnectivity();
    return result.isNotEmpty;
  }

  /// 检查是否连接WiFi
  Future<bool> isConnectedToWifi() async {
    final result = await _connectivity.checkConnectivity();
    if(result.isNotEmpty){
      return result.contains(ConnectivityResult.wifi);
    }else{
      return false;
    }
  }

  /// 检查是否连接移动网络（4G/5G等）
  Future<bool> isConnectedToMobile() async {
    final result = await _connectivity.checkConnectivity();
    if(result.isNotEmpty){
      return result.contains(ConnectivityResult.mobile);
    }else{
      return false;
    }
  }

  /// 监听网络状态变化（返回流，可在页面中监听）
  Stream<List<ConnectivityResult>> onNetworkStateChanged() {
    return _connectivity.onConnectivityChanged;
  }
}